var kgpList = "#kgpList", kgpType = "#kgpType", kgpKey = "#kgpKey", kgpDialog = "#kgpDialog", kform = "#kform", kgpCmmv = "#kgpCmmv";

function kgpListPage(param) {
	param.pgCt = param.page;
	param.pgSz = param.rows;
	delete param.page;
	delete param.rows;
	return param;
}

function kgpDatagridLoader(param, success, error) {
	let tgpType = $(kgpType).combobox("getValue");
	let tgpKey = $(kgpKey).textbox("getValue");
	if ("" != $.trim(tgpType)) param.cmmv = tgpType;
	if ("" != $.trim(tgpKey)) param.pkey = tgpKey;
	
	ajaxPost(reqRoute.gparamPage, kgpListPage(param), function(resultData) {
		success(resultData);
	});
}

function kgpSearch() {
	$(kgpList).datagrid('load', {});
}

function kgpReset() {
	$(kgpType).combobox("clear");
	$(kgpKey).textbox("clear");
	kgpSearch();
}

function kgpOpenDialog(id) {
	var tmpUrl = "", kgpDescribes = "#kgpDescribes", kgpfKey = "#kgpfKey", kgpValue = "#kgpValue";
	if (null == id) {
		$(kgpCmmv).combobox("select", "GLOBAL");
		$(kgpfKey).textbox("clear");
		$(kgpValue).textbox("clear");
		$(kgpDescribes).textbox("clear");
		tmpUrl = reqRoute.gparamAdd;
	} else {
		tmpUrl = reqRoute.gparamEdit;
		$.get(reqRoute.gparamById, { id: id }, function(resultData) {
			try {
				if (resultData.status == successCode) {
					$(kform).form('load', resultData.data);
				} else {
					$.messager.alert({ title: '提示', msg: resultData.message, icon: 'error' });
				}
			} catch(e) {}
		});
	}
	
	$(kgpDialog).dialog({
		title: '新增',
		width: 400,
		buttons: [{
			text: '保存',
			iconCls: 'icon-ok',
			handler: function() {
				if ($(kform).form('enableValidation').form('validate')) {
					ajaxPost(tmpUrl, formData(kform), function(resultData) {
						$(kgpDialog).dialog('close');
						if (resultData.status == successCode) {
							kgpSearch();
						} else {
							$.messager.alert({ title: '提示', msg: resultData.message, icon: 'error' });
						}
					});
				}
			}
		},
		{
			text: '取消',
			handler: function(){
				$(kgpDialog).dialog('close');
			}
		}]
	});
	$(kform).form('disableValidation');
	$(kgpDialog).dialog('open');
}

function kgpDelete(id, pkey) {
	$.messager.confirm('提示', '确定要删除[' + pkey + ']吗？', function(r) {
	    if (r) {
	    	$.get(reqRoute.gparamDel, { id: id }, function(resultData) {
	    		if (resultData.status == successCode) {
	    			kgpSearch();
	    		} else {
	    			$.messager.alert("提示", resultData.message, "error");
	    		}
	    	});
	    }
	});
}